package vn.edu.ptithcm.pim.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.Project;
import vn.edu.ptithcm.pim.dom.Task;
import vn.edu.ptithcm.pim.dom.User;
import vn.edu.ptithcm.pim.service.ProjectService;
import vn.edu.ptithcm.pim.service.TaskService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;
import vn.edu.ptithcm.pim.util.XMLUtil;

@SuppressWarnings("deprecation")
public class GanttController extends SimpleFormController {
	private UserService userService;
	private ProjectService projectService;
	private TaskService taskService;

	Log log = LogFactory.getLog(GanttController.class);

	public GanttController() {
		super();
		setSessionForm(true);
		setCommandName("");
		setCommandClass(Object.class);
		setFormView(UrlConstants.GANTT_URL);
		setSuccessView(UrlConstants.GANTT_URL);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new Object();
	}

	@Override
	public void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		return showForm(request, response, errors);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
		int pnum = ParamUtil.getInteger(request, "pnum", 0);
		Map<String, Object> bean = new HashMap<String, Object>();
		if (userService.isSignedIn(request)) {
			Project project = projectService.findByNumber(pnum);
			bean.put("project", project);
			String loginName = userService.getCurrentLogin(request);
			User user = userService.findByLoginName(loginName);
			bean.put("user", user);
			bean.put("isSigned", true);
			bean.put("tab", "gantt");
			bean.put(Constants.LOGIN_NAME, loginName);
			bean.put(Constants.USER_NAME, userService.getUsername(loginName));
			bean.put("isAdmin", userService.isAdmin(loginName));

			List<Task> tasks = taskService.filterByProjectId(
					project.getProjectid(), 20, 0);
			String xmlFilePath = XMLUtil.buildXMLFromProject(request, project,
					tasks, Constants.UPLOADED_FILE_FOLDER);

			String path = request.getSession().getServletContext()
					.getRealPath("");
			int index = StringUtils.indexOf(path, "pim");
			path = path.substring(0, index);

			bean.put("xmlFilePath", xmlFilePath);

		} else {
			String redirectLink = UrlConstants.GANTT_URL + UrlConstants.HTM
					+ "?pnum=" + pnum;
			request.getSession().setAttribute("redirectLink", redirectLink);
			response.sendRedirect(UrlConstants.HOME_PAGE_URL + UrlConstants.HTM);
		}
		request.setAttribute("bean", bean);

		return super.showForm(request, response, errors);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

}
